<?php
class YoutubeFetcher extends AbstractFetcher {
	/**
	 * @var Zend_Gdata_YouTube_VideoEntry
	 */
	private $entry = null;
	
	/**
	 * @see AbstractFetcher#setContentInformation()
	 */
	protected function setContentInformation(){
		if(preg_match('/http:\/\/.*youtube\..{1,5}\/watch\?.*v=([^\&]*)/', $this->url, $match) && count($match) == 2){
			$this->contentInformation = $match[1];
			if($this->initYT()){
				return true;
			}
			else{
				return false;
			}
		}
		else{
			return false;
		}
	}
	
	/**
	 * @see AbstractFetcher#setTitle()
	 */
	protected function setTitle(){
		$this->title = $this->entry->getVideoTitle();
	}
	
	/**
	 * @see AbstractFetcher#setDescription()
	 */
	protected function setDescription(){
		$this->description = $this->entry->getVideoDescription();
	}
	
	/**
	 * @see AbstractFetcher#setPreviewImages()
	 */
	protected function setPreviewImages(){
		$thumbs = $this->entry->getVideoThumbnails();
		foreach($thumbs as $thumb){
			if(count($this->previewImages) >= MAX_PREVIEWS_PER_GET){
				break;
			}
			$this->previewImages[] = $thumb['url'];
		}
	}
	
	/**
	 * initializes the videoEntryObject, returns false, if video is not found
	 * @return void
	 */
	private function initYT(){
		if($this->entry === false || $this->contentInformation == ''){
			return false;
		}
		elseif($this->entry === null){
			try{
				require_once 'Zend/Loader.php'; 
				Zend_Loader::loadClass('Zend_Gdata_YouTube');
				$yt = new Zend_Gdata_YouTube();
				$this->entry = $yt->getVideoEntry($this->contentInformation);
				return true;
			}catch(Exception $e){
				$this->entry = false;
				return false;
			}
		}
		else{
			return true;
		}
	}
}